<?php
/**
*File Name:common.php
*Date:2016年3月1日下午6:16:43
*Copyright  (c)2016,783188184@qq.com All Rights Reserved
*____________________________________________________________________________________________________
*/
/*** 把返回的数据集转换成Tree注意这里的表中数据主键id一定要从1开始因为0是特殊的
 +----------------------------------------------------------
 * @param string $pid parent标记字段
 * @param string $level level标记字段
 * @return array 
 */
function getlists($p_id=0,&$lists=array(),$deep=0){
	$m = M("vterm");
	$arr = $m->where("p_id={$p_id}")->select();
	$count = count($arr);
	//考虑到哟可能没有子栏目所以用了empty
	for($i=0;$i<$count&&(!empty($arr));$i++){
			$arr[$i]['term_name'] = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$deep).'|--'.$arr[$i]['term_name'];
			$lists[] = $arr[$i];
		
				getLists($arr[$i]["id"],$lists,++$deep);
				--$deep;			
				
	}
	return $lists;
}

/*** 把post传输过来的下载名称和下载链接数组进行处理拼接成字符串
 +----------------------------------------------------------
 * @param array $path1 名称数组
 * @param array $path2 下载链接数组
 * @return string 或者 false
 */
function splitpath($path1,$path2,$downtype = "迅雷下载"){
	$name = array_filter($path1);
	$downpath = array_filter($path2);
	if($name && $downpath){
		$str = '';
		foreach($name as $k => $value){
			if(empty($str)){
				$str = $downtype.'::::';
			}
			if(!empty($downpath[$k])){
				//这里在从数据库取出后要去掉右边的$$$$
				$str = $str.$value.'||||'.$downpath[$k].'$$$$';
			}else{
				continue;
			}
		}
		$str = rtrim($str,'$$$$');
	}
	if(isset($str)){
		return $str;
	}else{
		return false;
	}
}

/*** 把将数据库中的路径分解成数组
 +----------------------------------------------------------
 * @param array $path 名称数组 $id第几组下载地址
 * @return string 或者 false
 */
function explodepath($path,$id){
	$downpath = explode('::::',$path);
	$pathinfo['v_downterm'.$id] = $downpath['0'];
	$patharray = explode('$$$$',$downpath['1']);
	foreach($patharray as $k => $value){
		$arr = explode('||||',$value);
		$downpath1['name'] = $arr['0'];//名称
		$downpath1['url'] = $arr['1'];//下载地址
		$nameurl[] = $downpath1;
	}
	$pathinfo['nameurl'.$id] = $nameurl;
	return $pathinfo;
}
